﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data; 
using System.Data.Common;
using WMSADBUtility;
using WMSAML.SY;

namespace WMSADL.SY
{
    public class TPermissions_DAL
    {
        /// <summary>
        /// 
        /// </summary>
        public int Add(TPermissions_Entity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TPermissions(");
            strSql.Append("MoudleID,Type,Display)");

            strSql.Append(" values (");
            strSql.Append("@MoudleID,@Type,@Display)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "MoudleID", DbType.Int32, entity.MoudleID);
            db.AddInParameter(dbCommand, "Type", DbType.Int32, entity.Type);
            db.AddInParameter(dbCommand, "Display", DbType.Int32, entity.Display);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM TPermissions ");
            strSql.Append(" WHERE ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "ID", DbType.Int32, id);
                int rows = db.ExecuteNonQuery(dbCommand);

                if (rows > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(TPermissions_Entity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE TPermissions SET ");
            strSql.Append("MoudleID=@MoudleID,");
            strSql.Append("Type=@Type,");
            strSql.Append("Display=@Display");
            strSql.Append(" WHERE ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "ID", DbType.Int32, entity.ID);
                db.AddInParameter(dbCommand, "MoudleID", DbType.Int32, entity.MoudleID);
                db.AddInParameter(dbCommand, "Type", DbType.Int32, entity.Type);
                db.AddInParameter(dbCommand, "Display", DbType.Int32, entity.Display);
                int rows = db.ExecuteNonQuery(dbCommand);

                if (rows > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// ByID
        /// </summary>
        public bool ExistsByID(int id)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(1) FROM TPermissions WHERE ID=@ID ");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "ID", DbType.Int32, id);
                int cmdresult;
                object obj = db.ExecuteScalar(dbCommand);
                if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                {
                    cmdresult = 0;
                }
                else
                {
                    cmdresult = int.Parse(obj.ToString());
                }
                if (cmdresult == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        /// <summary>
        /// Bytype
        /// </summary>
        public bool ExistsByType(int type)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(1) FROM TPermissions WHERE type=@type ");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "type", DbType.Int32, type);
                int cmdresult;
                object obj = db.ExecuteScalar(dbCommand);
                if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                {
                    cmdresult = 0;
                }
                else
                {
                    cmdresult = int.Parse(obj.ToString());
                }
                if (cmdresult == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        public TPermissions_Entity GetEntityByID(int moudleid,int type)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT ID,MoudleID,Type,Display FROM TPermissions ");
            strSql.Append(" WHERE Type=@Type AND MoudleID=@MoudleID");
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "MoudleID", DbType.String, moudleid);
                db.AddInParameter(dbCommand, "Type", DbType.String, type);
                TPermissions_Entity entity = new TPermissions_Entity();
                using (IDataReader dataReader = db.ExecuteReader(dbCommand))
                {
                    if (dataReader.Read())
                    {
                        entity.ID = Convert.ToInt32(dataReader["ID"].ToString());
                        entity.MoudleID = Convert.ToInt32(dataReader["MoudleID"].ToString());
                        entity.Type = Convert.ToInt32(dataReader["Type"].ToString());
                        entity.Display = Convert.ToInt32(dataReader["Display"].ToString());
                    }
                }
                return entity;
            }
        }
    }
}
